﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace RaceGame2013
{
    class CButton
    {
        // Declare variables for this class
        Texture2D texture;
        Vector2 position;
        Rectangle rectangle;

        // This indicates when the mouse is over the button, it's is actually there
        Color colour = new Color(255, 5, 100, 255);

        public Vector2 size;

        public CButton(Texture2D newTexture, GraphicsDevice graphics)
        {
            texture = newTexture;

            // ScreenW =    800, ScreenH    = 600
            // Imgw         100, ImgH       = 20
            size = new Vector2(graphics.Viewport.Width / 4, graphics.Viewport.Height / 15);
        }

        public CButton(Texture2D newTexture, GraphicsDevice graphics, int width, int height)
        {
            texture = newTexture;

            // ScreenW =    800, ScreenH    = 600
            // Imgw         100, ImgH       = 20
            size = new Vector2(width, height);
        }

        bool down;
        public bool isClicked;
        public void Update(MouseState mouse)
        {
            rectangle = new Rectangle((int)position.X, (int)position.Y,
            (int)size.X, (int)size.Y);

            Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);

            if (mouseRectangle.Intersects(rectangle))
            {
                // Loop
                if (colour.A == 255) down = false;
                if (colour.A == 0) down = true;
                if (down) colour.A += 3; else colour.A -= 3;
                if (mouse.LeftButton == ButtonState.Pressed) isClicked = true;

            }
            else if (colour.A < 255)
            {
                // When the mouse is away from the button, the button go back to fully visable
                colour.A += 3;
                isClicked = false;
            }
        }

        public void setPosition(Vector2 newPosition)
        {
            position = newPosition;
        }

        public void Draw(SpriteBatch spritebatch)
        {
            spritebatch.Draw(texture, rectangle, colour);
        }
    }
}
